package com.apobates.jforum2.troll.forum.stub.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * 版块
 */
public class BoardVo implements Serializable,Comparable<BoardVo>{
    // 版块ID
    private long id;
    // 名称
    private String title;
    // 描述
    private String description;
    // 创建日期
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime entryDateTime;
    // 图标
    private String imageAddr;
    // 版块组/卷的ID
    private int volumes;
    // 状态
    private int status;
    private String statusTitle;
    // 显示顺序
    private int ranking;
    // 访问地址
    private String link;
    // 状态图标
    private String icoStatus;
    private final static Pattern BOARD_URL_PARSE = Pattern.compile("/board/(.*).xhtml");
    public static BiFunction<Long, Integer, String> produceLink = (Long boardId, Integer boardgroupId) -> {
        return String.format("/board/%d-%d.xhtml", boardId, boardgroupId);
    };
    public BoardVo() {
    }
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public LocalDateTime getEntryDateTime() {
        return entryDateTime;
    }

    public void setEntryDateTime(LocalDateTime entryDateTime) {
        this.entryDateTime = entryDateTime;
    }

    public String getImageAddr() {
        return imageAddr;
    }

    public void setImageAddr(String imageAddr) {
        this.imageAddr = imageAddr;
    }

    public int getVolumes() {
        return volumes;
    }

    public void setVolumes(int volumes) {
        this.volumes = volumes;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getStatusTitle() {
        return statusTitle;
    }

    public void setStatusTitle(String statusTitle) {
        this.statusTitle = statusTitle;
    }

    public int getRanking() {
        return ranking;
    }

    public void setRanking(int ranking) {
        this.ranking = ranking;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getIcoStatus() {
        return icoStatus;
    }

    public void setIcoStatus(String icoStatus) {
        this.icoStatus = icoStatus;
    }

    @JsonIgnore
    public boolean isNormal() {
        return 4 == status;
    }
    @JsonIgnore
    @Override
    public int compareTo(BoardVo o) {
        return getRanking() - o.getRanking();
    }

    public static Optional<BoardVo> parseLink(String link){
        String[] params = {};
        try {
            params = link.split("-");
        } catch (NullPointerException | PatternSyntaxException e) {
            //throw new IllegalStateException("构造版块的ActiveDirectoryConnector实例参数不合法");
        }
        if (params.length == 0) {
            //throw new IllegalStateException("构造版块的ActiveDirectoryConnector实例失败");
            return Optional.empty();
        }
        //话题: x-x-x
        //版块: x-x
        //版块组: x
        if (params.length == 1) {
            BoardVo board = new BoardVo();
            int volumesId = -1;
            try{
                volumesId = Integer.valueOf(params[0]);
            }catch (NumberFormatException e){}
            board.setVolumes(volumesId);
            return Optional.of(board);
        }
        if (params.length == 2) {
            BoardVo board = new BoardVo();
            long id=0L;
            try{
                id = Long.valueOf(params[0]);
            }catch(NumberFormatException e){}
            board.setId(id);
            //
            int volumesId = -1;
            try{
                volumesId = Integer.valueOf(params[1]);
            }catch (NumberFormatException e){}
            board.setVolumes(volumesId);
            //board.setId(Commons.stringToLong(params[0], 0L));
            //board.setVolumes(Commons.stringToInteger(params[1], -1));
            return Optional.of(board);
        }
        if (params.length == 3) {
            BoardVo board = new BoardVo();
            long id=0L;
            try{
                id = Long.valueOf(params[1]);
            }catch(NumberFormatException e){}
            board.setId(id);
            //
            int volumesId = -1;
            try{
                volumesId = Integer.valueOf(params[2]);
            }catch (NumberFormatException e){}
            board.setVolumes(volumesId);
            //board.setId(Commons.stringToLong(params[1], 0L));
            //board.setVolumes(Commons.stringToInteger(params[2], -1));
            return Optional.of(board);
        }
        return Optional.empty();
    }
    public static Optional<BoardVo> parseLink(String requestRefString, String siteDomain) {
        if (requestRefString == null || !requestRefString.startsWith(siteDomain)) { // 只允许本站连接
            return Optional.empty();
        }
        Matcher m = BOARD_URL_PARSE.matcher(requestRefString);
        String connectValue = null;
        if (m.find()) {
            connectValue = m.group(1);
        }
        try {
            if (connectValue != null && connectValue.contains("volumes/")) { //从版块组->版块
                connectValue = connectValue.replace("volumes/", "");
            }
        } catch (NullPointerException e) {}
        return parseLink(connectValue);
    }
}